1. Project Clover database Sun Jul 21 2024 22:37:57 UTC
  2. Package org.joda.time.chrono

File AssembledChronology.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
83% of files have more coverage

Code metrics

162
178
48
2
565
433
137
0.77
3.71
24
2.85

Classes

Class Line # Actions
AssembledChronology 35 104 0% 96 79
0.6652542466.5%
AssembledChronology.Fields 396 74 0% 41 73
0.519736852%
 

Contributing tests

This file is covered by 2831 tests. .

Source view

1    /*
2    * Copyright 2001-2005 Stephen Colebourne
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.joda.time.chrono;
17   
18    import java.io.IOException;
19    import java.io.ObjectInputStream;
20   
21    import org.joda.time.Chronology;
22    import org.joda.time.DateTimeField;
23    import org.joda.time.DateTimeZone;
24    import org.joda.time.DurationField;
25   
26    /**
27    * Abstract Chronology that enables chronologies to be assembled from
28    * a container of fields.
29    * <p>
30    * AssembledChronology is thread-safe and immutable.
31    *
32    * @author Brian S O'Neill
33    * @since 1.0
34    */
 
35    public abstract class AssembledChronology extends BaseChronology {
36   
37    private static final long serialVersionUID = -6728465968995518215L;
38   
39    private final Chronology iBase;
40    private final Object iParam;
41   
42    private transient DurationField iMillis;
43    private transient DurationField iSeconds;
44    private transient DurationField iMinutes;
45    private transient DurationField iHours;
46    private transient DurationField iHalfdays;
47   
48    private transient DurationField iDays;
49    private transient DurationField iWeeks;
50    private transient DurationField iWeekyears;
51    private transient DurationField iMonths;
52    private transient DurationField iYears;
53    private transient DurationField iCenturies;
54    private transient DurationField iEras;
55   
56    private transient DateTimeField iMillisOfSecond;
57    private transient DateTimeField iMillisOfDay;
58    private transient DateTimeField iSecondOfMinute;
59    private transient DateTimeField iSecondOfDay;
60    private transient DateTimeField iMinuteOfHour;
61    private transient DateTimeField iMinuteOfDay;
62    private transient DateTimeField iHourOfDay;
63    private transient DateTimeField iClockhourOfDay;
64    private transient DateTimeField iHourOfHalfday;
65    private transient DateTimeField iClockhourOfHalfday;
66    private transient DateTimeField iHalfdayOfDay;
67   
68    private transient DateTimeField iDayOfWeek;
69    private transient DateTimeField iDayOfMonth;
70    private transient DateTimeField iDayOfYear;
71    private transient DateTimeField iWeekOfWeekyear;
72    private transient DateTimeField iWeekyear;
73    private transient DateTimeField iWeekyearOfCentury;
74    private transient DateTimeField iMonthOfYear;
75    private transient DateTimeField iYear;
76    private transient DateTimeField iYearOfEra;
77    private transient DateTimeField iYearOfCentury;
78    private transient DateTimeField iCenturyOfEra;
79    private transient DateTimeField iEra;
80   
81    // Bit set determines which base fields are used
82    // bit 1 set: hourOfDay, minuteOfHour, secondOfMinute, and millisOfSecond fields
83    // bit 2 set: millisOfDayField
84    // bit 3 set: year, monthOfYear, and dayOfMonth fields
85    private transient int iBaseFlags;
86   
87    /**
88    * Constructor calls the assemble method, enabling subclasses to define its
89    * supported fields. If a base chronology is supplied, the field set
90    * initially contains references to each base chronology field.
91    * <p>
92    * Other methods in this class will delegate to the base chronology, if it
93    * can be determined that the base chronology will produce the same results
94    * as AbstractChronology.
95    *
96    * @param base optional base chronology to copy initial fields from
97    * @param param optional param object available for assemble method
98    */
 
99  384 toggle protected AssembledChronology(Chronology base, Object param) {
100  384 iBase = base;
101  384 iParam = param;
102  384 setFields();
103    }
104   
 
105  94332 toggle public DateTimeZone getZone() {
106  94332 Chronology base;
107  ? if ((base = iBase) != null) {
108  94332 return base.getZone();
109    }
110  0 return null;
111    }
112   
 
113  321 toggle public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
114    int millisOfDay)
115    throws IllegalArgumentException
116    {
117  321 Chronology base;
118  ? if ((base = iBase) != null && (iBaseFlags & 6) == 6) {
119    // Only call specialized implementation if applicable fields are the same.
120  288 return base.getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
121    }
122  33 return super.getDateTimeMillis(year, monthOfYear, dayOfMonth, millisOfDay);
123    }
124   
 
125  4368 toggle public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
126    int hourOfDay, int minuteOfHour,
127    int secondOfMinute, int millisOfSecond)
128    throws IllegalArgumentException
129    {
130  4368 Chronology base;
131  ? if ((base = iBase) != null && (iBaseFlags & 5) == 5) {
132    // Only call specialized implementation if applicable fields are the same.
133  4219 return base.getDateTimeMillis(year, monthOfYear, dayOfMonth,
134    hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
135    }
136  149 return super.getDateTimeMillis(year, monthOfYear, dayOfMonth,
137    hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
138    }
139   
 
140  480 toggle public long getDateTimeMillis(long instant,
141    int hourOfDay, int minuteOfHour,
142    int secondOfMinute, int millisOfSecond)
143    throws IllegalArgumentException
144    {
145  480 Chronology base;
146  ? if ((base = iBase) != null && (iBaseFlags & 1) == 1) {
147    // Only call specialized implementation if applicable fields are the same.
148  247 return base.getDateTimeMillis
149    (instant, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
150    }
151  233 return super.getDateTimeMillis
152    (instant, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond);
153    }
154   
 
155  832 toggle public final DurationField millis() {
156  832 return iMillis;
157    }
158   
 
159  359266 toggle public final DateTimeField millisOfSecond() {
160  359266 return iMillisOfSecond;
161    }
162   
 
163  2785234 toggle public final DateTimeField millisOfDay() {
164  2785234 return iMillisOfDay;
165    }
166   
 
167  862 toggle public final DurationField seconds() {
168  862 return iSeconds;
169    }
170   
 
171  17547 toggle public final DateTimeField secondOfMinute() {
172  17547 return iSecondOfMinute;
173    }
174   
 
175  2984 toggle public final DateTimeField secondOfDay() {
176  2984 return iSecondOfDay;
177    }
178   
 
179  993 toggle public final DurationField minutes() {
180  993 return iMinutes;
181    }
182   
 
183  23744 toggle public final DateTimeField minuteOfHour() {
184  23744 return iMinuteOfHour;
185    }
186   
 
187  527 toggle public final DateTimeField minuteOfDay() {
188  527 return iMinuteOfDay;
189    }
190   
 
191  1178 toggle public final DurationField hours() {
192  1178 return iHours;
193    }
194   
 
195  20337 toggle public final DateTimeField hourOfDay() {
196  20337 return iHourOfDay;
197    }
198   
 
199  495 toggle public final DateTimeField clockhourOfDay() {
200  495 return iClockhourOfDay;
201    }
202   
 
203  535 toggle public final DurationField halfdays() {
204  535 return iHalfdays;
205    }
206   
 
207  497 toggle public final DateTimeField hourOfHalfday() {
208  497 return iHourOfHalfday;
209    }
210   
 
211  513 toggle public final DateTimeField clockhourOfHalfday() {
212  513 return iClockhourOfHalfday;
213    }
214   
 
215  815 toggle public final DateTimeField halfdayOfDay() {
216  815 return iHalfdayOfDay;
217    }
218   
 
219  62259 toggle public final DurationField days() {
220  62259 return iDays;
221    }
222   
 
223  2730873 toggle public final DateTimeField dayOfWeek() {
224  2730873 return iDayOfWeek;
225    }
226   
 
227  3319439 toggle public final DateTimeField dayOfMonth() {
228  3319439 return iDayOfMonth;
229    }
230   
 
231  6084 toggle public final DateTimeField dayOfYear() {
232  6084 return iDayOfYear;
233    }
234   
 
235  1134 toggle public final DurationField weeks() {
236  1134 return iWeeks;
237    }
238   
 
239  21115 toggle public final DateTimeField weekOfWeekyear() {
240  21115 return iWeekOfWeekyear;
241    }
242   
 
243  742 toggle public final DurationField weekyears() {
244  742 return iWeekyears;
245    }
246   
 
247  4224 toggle public final DateTimeField weekyear() {
248  4224 return iWeekyear;
249    }
250   
 
251  496 toggle public final DateTimeField weekyearOfCentury() {
252  496 return iWeekyearOfCentury;
253    }
254   
 
255  50555 toggle public final DurationField months() {
256  50555 return iMonths;
257    }
258   
 
259  2733455 toggle public final DateTimeField monthOfYear() {
260  2733455 return iMonthOfYear;
261    }
262   
 
263  42348 toggle public final DurationField years() {
264  42348 return iYears;
265    }
266   
 
267  4805924 toggle public final DateTimeField year() {
268  4805924 return iYear;
269    }
270   
 
271  593 toggle public final DateTimeField yearOfEra() {
272  593 return iYearOfEra;
273    }
274   
 
275  569 toggle public final DateTimeField yearOfCentury() {
276  569 return iYearOfCentury;
277    }
278   
 
279  551 toggle public final DurationField centuries() {
280  551 return iCenturies;
281    }
282   
 
283  630 toggle public final DateTimeField centuryOfEra() {
284  630 return iCenturyOfEra;
285    }
286   
 
287  717 toggle public final DurationField eras() {
288  717 return iEras;
289    }
290   
 
291  636 toggle public final DateTimeField era() {
292  636 return iEra;
293    }
294   
295    /**
296    * Invoked by the constructor and after deserialization to allow subclasses
297    * to define all of its supported fields. All unset fields default to
298    * unsupported instances.
299    *
300    * @param fields container of fields
301    */
302    protected abstract void assemble(Fields fields);
303   
304    /**
305    * Returns the same base chronology as passed into the constructor.
306    */
 
307  135602 toggle protected final Chronology getBase() {
308  135602 return iBase;
309    }
310   
311    /**
312    * Returns the same param object as passed into the constructor.
313    */
 
314  18849 toggle protected final Object getParam() {
315  18849 return iParam;
316    }
317   
 
318  483 toggle private void setFields() {
319  483 Fields fields = new Fields();
320  483 if (iBase != null) {
321  430 fields.copyFieldsFrom(iBase);
322    }
323  483 assemble(fields);
324   
325    {
326  483 DurationField f;
327  ? iMillis = (f = fields.millis) != null ? f : super.millis();
328  ? iSeconds = (f = fields.seconds) != null ? f : super.seconds();
329  ? iMinutes = (f = fields.minutes) != null ? f : super.minutes();
330  ? iHours = (f = fields.hours) != null ? f : super.hours();
331  ? iHalfdays = (f = fields.halfdays) != null ? f : super.halfdays();
332  ? iDays = (f = fields.days) != null ? f : super.days();
333  ? iWeeks = (f = fields.weeks) != null ? f : super.weeks();
334  ? iWeekyears = (f = fields.weekyears) != null ? f : super.weekyears();
335  ? iMonths = (f = fields.months) != null ? f : super.months();
336  ? iYears = (f = fields.years) != null ? f : super.years();
337  ? iCenturies = (f = fields.centuries) != null ? f : super.centuries();
338  ? iEras = (f = fields.eras) != null ? f : super.eras();
339    }
340   
341    {
342  483 DateTimeField f;
343  ? iMillisOfSecond = (f = fields.millisOfSecond) != null ? f : super.millisOfSecond();
344  ? iMillisOfDay = (f = fields.millisOfDay) != null ? f : super.millisOfDay();
345  ? iSecondOfMinute = (f = fields.secondOfMinute) != null ? f : super.secondOfMinute();
346  ? iSecondOfDay = (f = fields.secondOfDay) != null ? f : super.secondOfDay();
347  ? iMinuteOfHour = (f = fields.minuteOfHour) != null ? f : super.minuteOfHour();
348  ? iMinuteOfDay = (f = fields.minuteOfDay) != null ? f : super.minuteOfDay();
349  ? iHourOfDay = (f = fields.hourOfDay) != null ? f : super.hourOfDay();
350  ? iClockhourOfDay = (f = fields.clockhourOfDay) != null ? f : super.clockhourOfDay();
351  ? iHourOfHalfday = (f = fields.hourOfHalfday) != null ? f : super.hourOfHalfday();
352  ? iClockhourOfHalfday = (f = fields.clockhourOfHalfday) != null ? f : super.clockhourOfHalfday();
353  ? iHalfdayOfDay = (f = fields.halfdayOfDay) != null ? f : super.halfdayOfDay();
354  ? iDayOfWeek = (f = fields.dayOfWeek) != null ? f : super.dayOfWeek();
355  ? iDayOfMonth = (f = fields.dayOfMonth) != null ? f : super.dayOfMonth();
356  ? iDayOfYear = (f = fields.dayOfYear) != null ? f : super.dayOfYear();
357  ? iWeekOfWeekyear = (f = fields.weekOfWeekyear) != null ? f : super.weekOfWeekyear();
358  ? iWeekyear = (f = fields.weekyear) != null ? f : super.weekyear();
359  ? iWeekyearOfCentury = (f = fields.weekyearOfCentury) != null ? f : super.weekyearOfCentury();
360  ? iMonthOfYear = (f = fields.monthOfYear) != null ? f : super.monthOfYear();
361  ? iYear = (f = fields.year) != null ? f : super.year();
362  ? iYearOfEra = (f = fields.yearOfEra) != null ? f : super.yearOfEra();
363  ? iYearOfCentury = (f = fields.yearOfCentury) != null ? f : super.yearOfCentury();
364  ? iCenturyOfEra = (f = fields.centuryOfEra) != null ? f : super.centuryOfEra();
365  ? iEra = (f = fields.era) != null ? f : super.era();
366    }
367   
368  483 int flags;
369  483 if (iBase == null) {
370  53 flags = 0;
371    } else {
372  430 flags =
373  430 ((iHourOfDay == iBase.hourOfDay() &&
374    iMinuteOfHour == iBase.minuteOfHour() &&
375    iSecondOfMinute == iBase.secondOfMinute() &&
376    iMillisOfSecond == iBase.millisOfSecond() ) ? 1 : 0) |
377   
378  430 ((iMillisOfDay == iBase.millisOfDay()) ? 2 : 0) |
379   
380  430 ((iYear == iBase.year() &&
381    iMonthOfYear == iBase.monthOfYear() &&
382    iDayOfMonth == iBase.dayOfMonth() ) ? 4 : 0);
383    }
384   
385  483 iBaseFlags = flags;
386    }
387   
 
388  99 toggle private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
389  99 in.defaultReadObject();
390  99 setFields();
391    }
392   
393    /**
394    * A container of fields used for assembling a chronology.
395    */
 
396    public static final class Fields {
397    public DurationField millis;
398    public DurationField seconds;
399    public DurationField minutes;
400    public DurationField hours;
401    public DurationField halfdays;
402   
403    public DurationField days;
404    public DurationField weeks;
405    public DurationField weekyears;
406    public DurationField months;
407    public DurationField years;
408    public DurationField centuries;
409    public DurationField eras;
410   
411    public DateTimeField millisOfSecond;
412    public DateTimeField millisOfDay;
413    public DateTimeField secondOfMinute;
414    public DateTimeField secondOfDay;
415    public DateTimeField minuteOfHour;
416    public DateTimeField minuteOfDay;
417    public DateTimeField hourOfDay;
418    public DateTimeField clockhourOfDay;
419    public DateTimeField hourOfHalfday;
420    public DateTimeField clockhourOfHalfday;
421    public DateTimeField halfdayOfDay;
422   
423    public DateTimeField dayOfWeek;
424    public DateTimeField dayOfMonth;
425    public DateTimeField dayOfYear;
426    public DateTimeField weekOfWeekyear;
427    public DateTimeField weekyear;
428    public DateTimeField weekyearOfCentury;
429    public DateTimeField monthOfYear;
430    public DateTimeField year;
431    public DateTimeField yearOfEra;
432    public DateTimeField yearOfCentury;
433    public DateTimeField centuryOfEra;
434    public DateTimeField era;
435   
 
436  483 toggle Fields() {
437    }
438   
439    /**
440    * Copy the supported fields from a chronology into this container.
441    */
 
442  444 toggle public void copyFieldsFrom(Chronology chrono) {
443    {
444  444 DurationField f;
445  ? if (isSupported(f = chrono.millis())) {
446  444 millis = f;
447    }
448  ? if (isSupported(f = chrono.seconds())) {
449  444 seconds = f;
450    }
451  ? if (isSupported(f = chrono.minutes())) {
452  444 minutes = f;
453    }
454  ? if (isSupported(f = chrono.hours())) {
455  444 hours = f;
456    }
457  ? if (isSupported(f = chrono.halfdays())) {
458  444 halfdays = f;
459    }
460  ? if (isSupported(f = chrono.days())) {
461  444 days = f;
462    }
463  ? if (isSupported(f = chrono.weeks())) {
464  444 weeks = f;
465    }
466  ? if (isSupported(f = chrono.weekyears())) {
467  444 weekyears = f;
468    }
469  ? if (isSupported(f = chrono.months())) {
470  444 months = f;
471    }
472  ? if (isSupported(f = chrono.years())) {
473  444 years = f;
474    }
475  ? if (isSupported(f = chrono.centuries())) {
476  444 centuries = f;
477    }
478  ? if (isSupported(f = chrono.eras())) {
479  0 eras = f;
480    }
481    }
482   
483    {
484  444 DateTimeField f;
485  ? if (isSupported(f = chrono.millisOfSecond())) {
486  444 millisOfSecond = f;
487    }
488  ? if (isSupported(f = chrono.millisOfDay())) {
489  444 millisOfDay = f;
490    }
491  ? if (isSupported(f = chrono.secondOfMinute())) {
492  444 secondOfMinute = f;
493    }
494  ? if (isSupported(f = chrono.secondOfDay())) {
495  444 secondOfDay = f;
496    }
497  ? if (isSupported(f = chrono.minuteOfHour())) {
498  444 minuteOfHour = f;
499    }
500  ? if (isSupported(f = chrono.minuteOfDay())) {
501  444 minuteOfDay = f;
502    }
503  ? if (isSupported(f = chrono.hourOfDay())) {
504  444 hourOfDay = f;
505    }
506  ? if (isSupported(f = chrono.clockhourOfDay())) {
507  444 clockhourOfDay = f;
508    }
509  ? if (isSupported(f = chrono.hourOfHalfday())) {
510  444 hourOfHalfday = f;
511    }
512  ? if (isSupported(f = chrono.clockhourOfHalfday())) {
513  444 clockhourOfHalfday = f;
514    }
515  ? if (isSupported(f = chrono.halfdayOfDay())) {
516  444 halfdayOfDay = f;
517    }
518  ? if (isSupported(f = chrono.dayOfWeek())) {
519  444 dayOfWeek = f;
520    }
521  ? if (isSupported(f = chrono.dayOfMonth())) {
522  444 dayOfMonth = f;
523    }
524  ? if (isSupported(f = chrono.dayOfYear())) {
525  444 dayOfYear = f;
526    }
527  ? if (isSupported(f = chrono.weekOfWeekyear())) {
528  444 weekOfWeekyear = f;
529    }
530  ? if (isSupported(f = chrono.weekyear())) {
531  444 weekyear = f;
532    }
533  ? if (isSupported(f = chrono.weekyearOfCentury())) {
534  444 weekyearOfCentury = f;
535    }
536  ? if (isSupported(f = chrono.monthOfYear())) {
537  444 monthOfYear = f;
538    }
539  ? if (isSupported(f = chrono.year())) {
540  444 year = f;
541    }
542  ? if (isSupported(f = chrono.yearOfEra())) {
543  444 yearOfEra = f;
544    }
545  ? if (isSupported(f = chrono.yearOfCentury())) {
546  444 yearOfCentury = f;
547    }
548  ? if (isSupported(f = chrono.centuryOfEra())) {
549  444 centuryOfEra = f;
550    }
551  ? if (isSupported(f = chrono.era())) {
552  444 era = f;
553    }
554    }
555    }
556   
 
557  5328 toggle private static boolean isSupported(DurationField field) {
558  5328 return field == null ? false : field.isSupported();
559    }
560   
 
561  10212 toggle private static boolean isSupported(DateTimeField field) {
562  10212 return field == null ? false : field.isSupported();
563    }
564    }
565    }